package edu.unf.cen6940.web;

import edu.unf.cen6940.dao.CategoryDAO;
import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.ThesisDAO;

import edu.unf.cen6940.domain.Category;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Thesis;

import edu.unf.cen6940.service.CategoryService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Category entities
 * 
 */

@Controller("CategoryController")
public class CategoryController {

	/**
	 * DAO injected by Spring that manages Category entities
	 * 
	 */
	@Autowired
	private CategoryDAO categoryDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Category entities
	 * 
	 */
	@Autowired
	private CategoryService categoryService;

	/**
	 * Edit an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/editCategoryFacultyresearchs")
	public ModelAndView editCategoryFacultyresearchs(@RequestParam Integer category_categoryId, @RequestParam Integer facultyresearchs_facultyResearchId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearchs_facultyResearchId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("facultyresearch", facultyresearch);
		mav.setViewName("category/facultyresearchs/editFacultyresearchs");

		return mav;
	}

	/**
	 * Select the child Facultyresearch entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteCategoryFacultyresearchs")
	public ModelAndView confirmDeleteCategoryFacultyresearchs(@RequestParam Integer category_categoryId, @RequestParam Integer related_facultyresearchs_facultyResearchId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("facultyresearch", facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs_facultyResearchId));
		mav.addObject("category_categoryId", category_categoryId);
		mav.setViewName("category/facultyresearchs/deleteFacultyresearchs");

		return mav;
	}

	/**
	 * View an existing Thesis entity
	 * 
	 */
	@RequestMapping("/selectCategoryThesises")
	public ModelAndView selectCategoryThesises(@RequestParam Integer category_categoryId, @RequestParam Integer thesises_thesisId) {
		Thesis thesis = thesisDAO.findThesisByPrimaryKey(thesises_thesisId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("thesis", thesis);
		mav.setViewName("category/thesises/viewThesises");

		return mav;
	}

	/**
	 * Create a new Thesis entity
	 * 
	 */
	@RequestMapping("/newCategoryThesises")
	public ModelAndView newCategoryThesises(@RequestParam Integer category_categoryId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("thesis", new Thesis());
		mav.addObject("newFlag", true);
		mav.setViewName("category/thesises/editThesises");

		return mav;
	}

	/**
	 * Edit an existing Category entity
	 * 
	 */
	@RequestMapping("/editCategory")
	public ModelAndView editCategory(@RequestParam Integer categoryIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		mav.setViewName("category/editCategory");

		return mav;
	}

	/**
	 * Show all Facultyresearch entities by Category
	 * 
	 */
	@RequestMapping("/listCategoryFacultyresearchs")
	public ModelAndView listCategoryFacultyresearchs(@RequestParam Integer categoryIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		mav.setViewName("category/facultyresearchs/listFacultyresearchs");

		return mav;
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@RequestMapping("/saveCategoryThesises")
	public ModelAndView saveCategoryThesises(@RequestParam Integer category_categoryId, @ModelAttribute Thesis thesises) {
		Category parent_category = categoryService.saveCategoryThesises(category_categoryId, thesises);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("category", parent_category);
		mav.setViewName("category/viewCategory");

		return mav;
	}
	
	/**
	 * Select an existing Category entity
	 * 
	 */
	@RequestMapping("/selectCategory")
	public String selectCategory(@RequestParam Integer categoryIdKey, Model model) {

		model.addAttribute("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		return "category/viewCategory";
	}

	/**
	 * Delete an existing Category entity
	 * 
	 */
	@RequestMapping("/deleteCategory")
	public String deleteCategory(@RequestParam Integer categoryIdKey) {
		Category category = categoryDAO.findCategoryByPrimaryKey(categoryIdKey);
		categoryService.deleteCategory(category);
		return "forward:/indexCategory";
	}

	/**
	 * Select the Category entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteCategory")
	public ModelAndView confirmDeleteCategory(@RequestParam Integer categoryIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		mav.setViewName("category/deleteCategory");

		return mav;
	}

	/**
	 * Create a new Category entity
	 * 
	 */
	@RequestMapping("/newCategory")
	public ModelAndView newCategory() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", new Category());
		mav.addObject("newFlag", true);
		mav.setViewName("category/editCategory");

		return mav;
	}

	/**
	 * Show all Category entities
	 * 
	 */
	@RequestMapping("/indexCategory")
	public ModelAndView listCategorys() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("categorys", categoryService.loadCategorys());

		mav.setViewName("category/listCategorys");

		return mav;
	}

	/**
	 * View an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/selectCategoryFacultyresearchs")
	public ModelAndView selectCategoryFacultyresearchs(@RequestParam Integer category_categoryId, @RequestParam Integer facultyresearchs_facultyResearchId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearchs_facultyResearchId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("facultyresearch", facultyresearch);
		mav.setViewName("category/facultyresearchs/viewFacultyresearchs");

		return mav;
	}

	/**
	 * Show all Thesis entities by Category
	 * 
	 */
	@RequestMapping("/listCategoryThesises")
	public ModelAndView listCategoryThesises(@RequestParam Integer categoryIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		mav.setViewName("category/thesises/listThesises");

		return mav;
	}

	/**
	 */
	@RequestMapping("/categoryController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Edit an existing Thesis entity
	 * 
	 */
	@RequestMapping("/editCategoryThesises")
	public ModelAndView editCategoryThesises(@RequestParam Integer category_categoryId, @RequestParam Integer thesises_thesisId) {
		Thesis thesis = thesisDAO.findThesisByPrimaryKey(thesises_thesisId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("thesis", thesis);
		mav.setViewName("category/thesises/editThesises");

		return mav;
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@RequestMapping("/deleteCategoryThesises")
	public ModelAndView deleteCategoryThesises(@RequestParam Integer category_categoryId, @RequestParam Integer related_thesises_thesisId) {
		ModelAndView mav = new ModelAndView();

		Category category = categoryService.deleteCategoryThesises(category_categoryId, related_thesises_thesisId);

		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("category", category);
		mav.setViewName("category/viewCategory");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/saveCategoryFacultyresearchs")
	public ModelAndView saveCategoryFacultyresearchs(@RequestParam Integer category_categoryId, @ModelAttribute Facultyresearch facultyresearchs) {
		Category parent_category = categoryService.saveCategoryFacultyresearchs(category_categoryId, facultyresearchs);

		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("category", parent_category);
		mav.setViewName("category/viewCategory");

		return mav;
	}

	/**
	 * Save an existing Category entity
	 * 
	 */
	@RequestMapping("/saveCategory")
	public String saveCategory(@ModelAttribute Category category) {
		categoryService.saveCategory(category);
		return "forward:/indexCategory";
	}

	/**
	 * Select the child Thesis entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteCategoryThesises")
	public ModelAndView confirmDeleteCategoryThesises(@RequestParam Integer category_categoryId, @RequestParam Integer related_thesises_thesisId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId));
		mav.addObject("category_categoryId", category_categoryId);
		mav.setViewName("category/thesises/deleteThesises");

		return mav;
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/deleteCategoryFacultyresearchs")
	public ModelAndView deleteCategoryFacultyresearchs(@RequestParam Integer category_categoryId, @RequestParam Integer related_facultyresearchs_facultyResearchId) {
		ModelAndView mav = new ModelAndView();

		Category category = categoryService.deleteCategoryFacultyresearchs(category_categoryId, related_facultyresearchs_facultyResearchId);

		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("category", category);
		mav.setViewName("category/viewCategory");

		return mav;
	}

	/**
	 * Create a new Facultyresearch entity
	 * 
	 */
	@RequestMapping("/newCategoryFacultyresearchs")
	public ModelAndView newCategoryFacultyresearchs(@RequestParam Integer category_categoryId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("category_categoryId", category_categoryId);
		mav.addObject("facultyresearch", new Facultyresearch());
		mav.addObject("newFlag", true);
		mav.setViewName("category/facultyresearchs/editFacultyresearchs");

		return mav;
	}

	/**
	 * Entry point to show all Category entities
	 * 
	 */
	public String indexCategory() {
		return "redirect:/indexCategory";
	}
	
	/**
	 * Search by term
	 * */
	@RequestMapping("/searchCategory")
	public String searchCategory(@RequestParam String searchTerm, Model model){
		model.addAttribute("categorys", categoryDAO.searchByTerm(searchTerm));
		return "category/listCategorys";
	}
}